#!/usr/bin/env python
# coding=utf-8
import vavava.util

def test(log):
    from PIL import Image
    import vavava.tesseract
    import pytesser.pytesser
    f = r'phototest.tif'
    f = r'untitled.png'
    f = r'.\pic\pic_1.bmp'
    #f = r'.\pic\images.jpg'
    #f = r'.\pic\phototest.tif'
    f = r'1353424990968'
    image = Image.open(f)
    #image.show()
    #image.save("./pic/test_pic.bmp","bmp")
    #r,g,b = image.split()
    #img = Image.merge("RGB", (r,g,b))
    #img.save(".\pic\test_pic.png","png")
    #untitled.png
    tmp = pytesser.pytesser.image_to_string(image)
    log.info(tmp)

def test2(log):
    import os
    from PIL import Image

    curr_dir = os.path.abspath("./pic")
    pics = []
    files = os.listdir("./pic")
    for f in files:
        pic_name = os.path.join(curr_dir,f)
        if f.endswith(".png") or f.endswith(".jpg") or f.endswith(".bmp"):
            pics.append(pic_name)

    import vavava.tesseract
    from vavava.tesseract import Tesseract4py
    path  = r'D:\_code\online\trunk\python\Src\tracker\spiders\tessdata'
    lang  = r'auspost'
    tess = Tesseract4py(path,lang)

    for pic_name in pics:
        log.info("get:%s"%pic_name)
        tessercat_result = tess.img_to_string(pic_name)
        if not tessercat_result:
            continue
        img = Image.open(pic_name)
        tessercat_result = tessercat_result.replace('\n','')
        tessercat_result = tessercat_result.replace(' ','')
        tessercat_result = os.path.join("./re", tessercat_result+".jpg")
        tessercat_result = os.path.abspath(tessercat_result)
        if len(tessercat_result) > 100:
            print(tessercat_result)
            continue
        log.info(tessercat_result)
        img.save(tessercat_result)
        print(tessercat_result)

if __name__ == '__main__':
    log = vavava.util.initlog("./log/223s.log")
    test2(log)

